package com.teaphy.readlist.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author teaphy
 * @desc
 * @since 20:56
 */
@WebFilter(urlPatterns = "/v1/login", filterName = "loginFilter")
public class LoginFilter implements Filter {

    /**
     * 容器加载时调用
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init loginFilter");
    }

    /**
     * 请求被拦截时调用
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("doFilter loginFilter");
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        HttpServletResponse servletResponse = (HttpServletResponse) response;

        String id = servletRequest.getParameter("id");

        if (id.equals("1")) {
            chain.doFilter(servletRequest, servletResponse);
        } else {
            System.out.println("被拦截了");
        }

    }

    /**
     * 容器被销毁时调用
     */
    @Override
    public void destroy() {
        System.out.println("destroy filter");
    }
}
